package com.ebelter.myehc.ui.fragments.historys;

import android.graphics.Color;
import android.util.TypedValue;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.ebelter.btcomlib.bases.fragment.BaseFragment;
import com.ebelter.btcomlib.models.eventbus.CommonEventBus;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.utils.StringUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.myehc.R;
import com.ebelter.myehc.adapters.historys.H_Temperature_A;
import com.ebelter.myehc.models.http.request.EhcNetUtils;
import com.ebelter.myehc.models.http.response.GetTemperature;
import com.ebelter.myehc.ui.activitys.HistoryActivity;

import org.greenrobot.eventbus.EventBus;

import static com.ebelter.btcomlib.common.Constants.IUser.AUTH_KEY;


public class H_Tiwen_Fragment extends BaseFragment {
    public static final String TAG = "H_Tiwen_Fragment";

    ListView listView;
    H_Temperature_A adapter;

    RelativeLayout no_history_ly;

    private int startPage = 1;
    private int pageSize=10;

    private long lastLoadingTime;

    View listFootView;
    TextView noMoreTv;

    @Override
    protected int setLayoutResouceId() {
        return R.layout.fragment_historys;
    }

    @Override
    protected void initView() {
        super.initView();
        FV();
        adapter=new H_Temperature_A(getContext());
        listView.setAdapter(adapter);

        listFootView = View.inflate(getContext(), R.layout.no_history, null);
        noMoreTv=listFootView.findViewById(R.id.no_data_tv);
        noMoreTv.setText(R.string.No_more);
        noMoreTv.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
        noMoreTv.setTextColor(Color.parseColor("#ffcccccc"));
        listView.addFooterView(listFootView);
        ViewUtils.hideView(listFootView);

        getHostoryListDatas(startPage,pageSize);

        listView.setOnScrollListener(new AbsListView.OnScrollListener(){
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState){
                // 当不滚动时
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    // 判断是否滚动到底部
                    if (view.getLastVisiblePosition() == view.getCount() - 1) {
                        //加载更多功能的代码
                        if (System.currentTimeMillis()-lastLoadingTime>3000){
                            LogUtils.i(TAG,"---------加载更多");
                            lastLoadingTime=System.currentTimeMillis();
                            getHostoryListDatas(++startPage,pageSize);
                        }
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            }
        });





    }

    private void FV() {
        listView = mRootView.findViewById(R.id.history_lv);
        no_history_ly = findViewById(R.id.no_history_ly);
    }




    public void getHostoryListDatas(final int startPage,final int pageSize) {
        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }
        showLoadingDialog(StringUtils.getResStr(R.string.loading));

        String authKey = UserSpUtil.readString(AUTH_KEY);
        EhcNetUtils.getInstance().getTemperaturePage(this, authKey, startPage, pageSize, new HttpResponse<GetTemperature>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, GetTemperature getTemperature, String s) {
                dissLoadingDialog();
                LogUtils.i(TAG,"---s = "+s);
                LogUtils.i(TAG,"---getBo = " + getTemperature);

                if (isRequestSuccess&&getTemperature.resultData!=null&&getTemperature.resultData.dataList!=null){
                    if (getTemperature.resultData.dataList.size()==0){
                        ViewUtils.displayView(listFootView);

                    }
                    if (adapter!=null)adapter.addData(getTemperature.resultData.dataList);
                }
                displayNoHistoryLy();
            }
        });


    }


    private void displayNoHistoryLy(){
        if (adapter!=null){
            CommonEventBus commonEventBus=new CommonEventBus(TAG, HistoryActivity.TAG,-1,null);
            if (adapter.getCount()==0){
                commonEventBus.cmd = 1;
                ViewUtils.displayView(no_history_ly);
                ViewUtils.goneView(noMoreTv);
            }else {
                commonEventBus.cmd = 0;
                ViewUtils.hideView(no_history_ly);
            }
            EventBus.getDefault().post(commonEventBus);
        }
    }



}
